<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
	global $sugar_config,$current_user;
    require_once('include/SugarPHPMailer.php');
    $test = new SugarPHPMailer();
    $test->AddAddress($_REQUEST['to_email']);
    if(!empty($_REQUEST['to_cc'])){
    	$cc_params = explode(',', $_REQUEST['to_cc']);
    	 foreach ($cc_params as $cc){
		    $test->AddCC($cc);
    	 }
    }
    $test->IsHTML(true);
    $test->Body = $_REQUEST['body'];
    $test->Subject = $_REQUEST['Subject'];
    $test->From = $_REQUEST['from'];
    $test->FromName = $_REQUEST['fromName'];
    if(!empty($_REQUEST['attachment']) && isset($_REQUEST['attachment']) && file_exists($sugar_config['upload_dir'].$_REQUEST["attachment"])){
	    $test->AddAttachment($sugar_config['upload_dir'].$_REQUEST['attachment'],$_REQUEST['attachment_name']);
    }
    $test->prepForOutbound();
    $test->setMailerForSystem();
    $test->Send();
    if(!empty($test->ErrorInfo)){
	    /*echo '<script>alert("<?php echo $test->ErrorInfo; ?>"); </script>';*/
    }else{
	    $email = new Email();
	    $email_id = create_guid();
	    $email_insert = "insert into emails(id, date_entered, date_modified, assigned_user_id, modified_user_id, created_by, deleted, date_sent, name, type, status, flagged, reply_to_status, intent,parent_id, parent_type)
	    values('{$email_id}', NOW(), NOW(), '{$current_user->id}', '{$current_user->id}', '{$current_user->id}', 0, NOW(),'{$_REQUEST['Subject']}', 'out', 'sent', '0', '0', 'pick', '{$_REQUEST['parent_id']}','{$_REQUEST['parent_type']}'  );";
	    
	    $email_bean_id = create_guid();
	    $emails_beans_insert = "INSERT INTO emails_beans(id, email_id, bean_id, bean_module, date_modified, deleted) 
	    values ('{$email_bean_id}', '{$email_id}', '{$_REQUEST['parent_id']}', '{$_REQUEST['parent_type']}', NOW(), 0);";
	    
	    $emails_text = "INSERT INTO emails_text (email_id, from_addr, to_addrs, cc_addrs, description, description_html, deleted) 
	    VALUES('{$email_id}', '{$_REQUEST['from']}', '{$_REQUEST['to_email']}', '{$_REQUEST['to_cc']}', '{$_REQUEST['body']}', '{$_REQUEST['body']}', 0);";
	    
	    $email->db->query($email_insert);
	    $email->db->query($emails_beans_insert);
	    $email->db->query($emails_text);
	    
	    if(isset($test->attachment)){
	    	$notes_id = create_guid();
	    	$notes_insert = "insert into notes(id, date_entered, date_modified, modified_user_id, created_by, name, filename, file_mime_type, parent_type, parent_id)
	    	values('{$_REQUEST['attachment']}',NOW(),NOW(),'{$current_user->id}','{$current_user->id}','{$_REQUEST['attachment_name']}','{$_REQUEST['attachment_name']}','application/xml','Emails','{$email_id}');";
	    	$email->db->query($notes_insert);
	    }
	    
    }
	    $action = $_REQUEST['return_action'];
	    $module = $_REQUEST['return_module'];
	    $record = $_REQUEST['return_id'];
	    
	    $header_URL = "Location: index.php?action=".$action."&module=".$module."&record=".$record."&ErrorInfo=".$test->ErrorInfo;
	    header($header_URL);
?>

